package cn.ygh.bbo.user.web.controller;

import cn.ygh.bbo.common.annotation.Log;
import cn.ygh.bbo.common.annotation.OperationType;
import cn.ygh.bbo.framerwork.core.Grid;
import cn.ygh.bbo.framerwork.core.ResultAjax;
import cn.ygh.bbo.framerwork.core.ClientInfo;
import cn.ygh.bbo.common.exception.TipException;
import cn.ygh.bbo.user.bean.UserOpretationLog;
import cn.ygh.bbo.user.service.UserOpretationLogService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 描述：操作日志  RestPageController
 * @author ygh
 */
@Controller
@RequestMapping("/restpage/userOpretationLog")
public class UserOpretationLogController {

    private final Logger logger= LoggerFactory.getLogger(getClass());

    @Autowired
    UserOpretationLogService userOpretationLogService;

    /**
     * 查询列表api 带总数
     * @param request 请求
     * @param response 响应
     * @param clientInfo 查询条件
     * @return
     */
    @RequiresPermissions("user:userOpretationLog:query")
    @ResponseBody
    @RequestMapping(value = "/query",method = RequestMethod.GET)
    public Grid grid(HttpServletRequest request, HttpServletResponse response, ClientInfo clientInfo, UserOpretationLog search){
        search.setOrderValue(clientInfo.realSort().trim());
        if (clientInfo.getPageNo()==0){
            throw new TipException("pageNum can not zero");
        }
        Grid grid=userOpretationLogService.listGrid(search,PageRequest.of(clientInfo.getPageNo(),clientInfo.getPageSize()));
        return grid;
    }

    /**
     * 详细
     * @param id
     * @return
     */
    @RequiresPermissions("user:userOpretationLog:show")
    @ResponseBody
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    public UserOpretationLog get(HttpServletRequest request, HttpServletResponse response, Integer id){
        UserOpretationLog result=userOpretationLogService.getById(id);
        return result;
    }


    /**
     * 创建数据api
     * @param request 请求
     * @param response 响应
     * @param obj 创建对象
     * @param errors 验证异常
     * @return
     */
    @Log(value="UserOpretationLog",operationId=OperationType.CREATE)
    @RequiresPermissions("user:userOpretationLog:create")
    @ResponseBody
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public ResultAjax create(HttpServletRequest request, HttpServletResponse response, @Valid UserOpretationLog obj, BindingResult errors){
        if (errors.hasErrors()){
            FieldError err0=(FieldError)errors.getAllErrors().get(0);
            return ResultAjax.fail("添加失败！"+err0.getField()+":"+err0.getDefaultMessage());
        }
        userOpretationLogService.save(obj);
        return ResultAjax.success(obj);
    }

    /**
     * 更新数据api
     * @param request 请求
     * @param response 响应
     * @param obj 更新对象
     * @param errors 验证异常
     * @return
     */
    @Log(value="UserOpretationLog",operationId=OperationType.UPDATE)
    @RequiresPermissions("user:userOpretationLog:update")
    @ResponseBody
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public ResultAjax update(HttpServletRequest request, HttpServletResponse response, @Valid UserOpretationLog obj, BindingResult errors){
        if (errors.hasErrors()){
            FieldError err0=(FieldError)errors.getAllErrors().get(0);
            return ResultAjax.fail("更新失败！"+err0.getField()+":"+err0.getDefaultMessage());
        }
        userOpretationLogService.update(obj);
        return ResultAjax.success(obj);
    }

    /**
     * 根据Ids删除
     * @param request 请求
     * @param response 响应
     * @param ids
     * @return
     */
    @Log(value="UserOpretationLog",operationId=OperationType.DEL)
    @RequiresPermissions("user:userOpretationLog:del")
    @ResponseBody
    @RequestMapping(value = "/del",method = RequestMethod.POST)
    public ResultAjax del(HttpServletRequest request, HttpServletResponse response, String ids){
        if (StringUtils.isBlank(ids)){
            return ResultAjax.fail("ids不能为空");
        }
        String[] idList=ids.split(",");
        userOpretationLogService.deleteByIds(idList);
        return ResultAjax.success();
    }

}
